package com.lottery.im.module.setting.presenter;

import android.app.Activity;
import com.lottery.im.MyApplication;
import com.lottery.im.api.ApiClient;
import com.lottery.im.api.observer.ShowTipObserver;
import com.lottery.im.base.BaseFragmentPresenter;
import com.lottery.im.module.setting.view.AddPwdView;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.FragmentEvent;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import java.util.concurrent.TimeUnit;

public class AddPwdPresenter extends BaseFragmentPresenter<AddPwdView> {

  public AddPwdPresenter(Activity activity,
      AddPwdView iView,
      LifecycleProvider<FragmentEvent> provider) {
    super(activity, iView, provider);
  }

  /**
   * 设置资金密码
   *
   * @param pwd 密码
   */
  public void updateWithdrawPassword(String pwd) {
    ApiClient.getApiRetrofitInstance()
        .updateWithdrawPassword(MyApplication.getInstance().getUserInfo().getUserId(),
            MyApplication.getInstance().getToken(), pwd)
        .throttleFirst(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new ShowTipObserver<Object>(activity) {
          @Override public void onSuccess(Object object) {
            iView.updatePasswordSuc();
          }
        });
  }

  /**
   * 设置验证密码
   *
   * @param pwd 密码
   */
  public void setEquipmentPwd(String pwd) {
    ApiClient.getApiRetrofitInstance()
        .setEquipmentPwd(MyApplication.getInstance().getUserInfo().getUserId(),
            MyApplication.getInstance().getToken(), pwd)
        .throttleFirst(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new ShowTipObserver<Object>(activity) {
          @Override public void onSuccess(Object object) {
            iView.updatePasswordSuc();
          }
        });
  }
}
